package com.drophair.jspwork.Servlet;

import com.drophair.jspwork.bean.Sqlgoods;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "AddGoodServlet", value = "/AddGoodServlet")
public class AddGoodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goodname="";
        double goodprice=0;

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        goodname=request.getParameter("goodname");
        goodprice=Double.parseDouble(request.getParameter("goodprice"));

        System.out.println("|||||||||||||||||"+goodname);
        Sqlgoods sqlgoods=new Sqlgoods();
        boolean sign=false;
        try {
            sign=sqlgoods.addGood(goodname,goodprice);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if(sign==true){
            request.setAttribute("error","《 "+goodname+" 》,添加成功");
        }else{
            request.setAttribute("error","添加失败");
        }
        request.getRequestDispatcher("/admin/main.jsp").forward(request,response);
    }
}
